package com.gexiaobao.pigeon.ui.fragment.mine.tools.query

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.RxTextTool
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.bean.SearchNameResponse
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.FragmentSearchNameBinding
import com.gexiaobao.pigeon.ui.adapter.SearchNameListAdapter
import com.gexiaobao.pigeon.viewmodel.MineToolsViwModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/8/3 15:55
 * @Describe    : 搜鸽主
 */
class FragmentSearchName : BaseFragment<MineToolsViwModel, FragmentSearchNameBinding>() {

    private val mAdapter: SearchNameListAdapter by lazy { SearchNameListAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>
    private var masterName = ""

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let { StatusBarUtil.setLightMode(it) }
        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
    }

    override fun initView(savedInstanceState: Bundle?) {
        masterName = arguments?.getString("masterName").toString()
        mDatabind.includeSearch.editQuery.setText(masterName)
        mDatabind.includeSearch.editQuery.hint = "请输入完整参赛姓名搜索"

        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.initFooter {
                /** 触发加载更多时请求数据 */
                showLoadingAntiShake()
                mViewModel.searchName(false, masterName)
            }
        }

        /** 状态页配置 */
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            /** 点击重试时触发的操作 */
            loadsir.showLoading()
            mViewModel.searchName(true, masterName)
        }

        /** 下拉刷新 */
        mDatabind.layoutList.swipeRefreshLayout.init {
            showLoadingAntiShake()
            mViewModel.searchName(true, masterName)
        }
        mAdapter.addChildClickViewIds(R.id.itemFindPigeonDetail)
        mAdapter.setOnItemChildClickListener { _, _, position ->
            nav().navigateAction(R.id.action_to_fragment_name_race, Bundle().apply {
                putString("title", mAdapter.data[position].name)
            })
        }

        mDatabind.includeSearch.editQuery.setOnEditorActionListener { _, i, _ ->
            when (i) {
                EditorInfo.IME_ACTION_SEARCH -> {
                    masterName = mDatabind.includeSearch.editQuery.text.toString().trim()
                    showLoading(getString(R.string.loading))
                    mViewModel.searchName(true, masterName)
                }

                else -> {}
            }
            false
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        showLoading(getString(R.string.loading))
        mViewModel.searchName(true, masterName)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.searchNameResult.observe(viewLifecycleOwner) {
            dismissLoadingAntiShake()
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
            eventViewModel.toNoticeRingId.value = masterName
//            showDataUI(it)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeSearch.ivBack, mDatabind.includeSearch.tvSearch) {
            when (it) {
                mDatabind.includeSearch.ivBack -> {
                    nav().navigateUp()
                }

                mDatabind.includeSearch.tvSearch -> {
                    masterName = mDatabind.includeSearch.editQuery.text.toString().trim()
                    showLoading(getString(R.string.loading))
                    mViewModel.searchName(true, masterName)
                }
            }
        }
    }

//    private fun showDataUI(it: ListDataUiState<SearchNameResponse.ListBean>) {
//        RxTextTool.getBuilder("")
//            .append("已为您找到同样或相似名称对应 ")
//            .append(it.total.toString()).setBold().setAppearance(50).setTextColor(Color.parseColor("#EB3E3E"))
//            .append(" 条记录")
//            .into(mDatabind.tvTotalData)
//    }
}